#include "MainWindow.h"

/*-----------------------------------------------------------------------------
 *  Contrói uma nova janela, conectando as ações dos widgets
 *-----------------------------------------------------------------------------*/
MainWindow::MainWindow()
{
    connect(trainLoadButton, SIGNAL(clicked()), this, SLOT(trainLoadButtonClick()));
    connect(trainClearButton, SIGNAL(clicked()), this, SLOT(trainClearButtonClick()));
    connect(trainNextButton, SIGNAL(clicked()), this, SLOT(trainNextButtonClick()));
    connect(trainPrevButton, SIGNAL(clicked()), this, SLOT(trainPrevButtonClick()));
    connect(trainButton, SIGNAL(clicked()), this, SLOT(trainButtonClick()));
    connect(trainPatternList, SIGNAL(itemSelectionChanged()), this, SLOT(trainPatternItemChange()));

    connect(testLoadButton, SIGNAL(clicked()), this, SLOT(testLoadButtonClick()));
    connect(testClearButton, SIGNAL(clicked()), this, SLOT(testClearButtonClick()));
    connect(testNextButton, SIGNAL(clicked()), this, SLOT(testNextButtonClick()));
    connect(testPrevButton, SIGNAL(clicked()), this, SLOT(testPrevButtonClick()));
    connect(testButton, SIGNAL(clicked()), this, SLOT(testButtonClick()));
    connect(testPatternList, SIGNAL(itemSelectionChanged()), this, SLOT(testPatternItemChange()));

    trainButton->setEnabled(false);
    trainClearButton->setEnabled(false);
    trainPrevButton->setEnabled(false);
    trainNextButton->setEnabled(false);

    testButton->setEnabled(false);
    testClearButton->setEnabled(false);
    testPrevButton->setEnabled(false);
    testNextButton->setEnabled(false);

    isTrained = isLoaded = false;
}

/*-----------------------------------------------------------------------------
 *  TRAIN: Ação ao clicar no botão 'Load'
 *-----------------------------------------------------------------------------*/
void MainWindow::trainLoadButtonClick()
{
    QFileDialog *dialog = new QFileDialog;
    dialog->show();
    connect(dialog, SIGNAL(fileSelected(const QString&)), this, SLOT(trainFileSelected(const QString&)));
}

/*-----------------------------------------------------------------------------
 *  TEST: Ação ao clicar no botão 'Load'
 *-----------------------------------------------------------------------------*/
void MainWindow::testLoadButtonClick()
{
    QFileDialog *dialog = new QFileDialog;
    dialog->show();
    connect(dialog, SIGNAL(fileSelected(const QString&)), this, SLOT(testFileSelected(const QString&)));
}

/*-----------------------------------------------------------------------------
 *  TRAIN: Ação ao clicar no botão 'Clear'
 *-----------------------------------------------------------------------------*/
void MainWindow::trainClearButtonClick()
{
    isTrained = false;

    trainPatternList->clear();
    weightTable->clear();
    trainPatternView->clear();

    trainButton->setEnabled(false);
    trainClearButton->setEnabled(false);
    trainPrevButton->setEnabled(false);
    trainNextButton->setEnabled(false);
    testButton->setEnabled(false);
}

/*-----------------------------------------------------------------------------
 *  TEST: Ação ao clicar no botão 'Clear'
 *-----------------------------------------------------------------------------*/
void MainWindow::testClearButtonClick()
{
    isLoaded = false;

    testPatternList->clear();
    testInPatternView->clear();
    testOutPatternView->clear();
    iterationsLabel->clear();

    testButton->setEnabled(false);
    testClearButton->setEnabled(false);
    testPrevButton->setEnabled(false);
    testNextButton->setEnabled(false);
}

/*-----------------------------------------------------------------------------
 *  TRAIN: Ação ao clicar no botão '>>'
 *-----------------------------------------------------------------------------*/
void MainWindow::trainNextButtonClick()
{
    nextPattern(trainPatternList);
}

/*-----------------------------------------------------------------------------
 *  TEST: Ação ao clicar no botão '>>'
 *-----------------------------------------------------------------------------*/
void MainWindow::testNextButtonClick()
{
    nextPattern(testPatternList);
}

/*-----------------------------------------------------------------------------
 *  TRAIN: Ação ao clicar no botão '<<'
 *-----------------------------------------------------------------------------*/
void MainWindow::trainPrevButtonClick()
{
    prevPattern(trainPatternList);
}

/*-----------------------------------------------------------------------------
 *  TEST: Ação ao clicar no botão '<<'
 *-----------------------------------------------------------------------------*/
void MainWindow::testPrevButtonClick()
{
    prevPattern(testPatternList);
}

/*-----------------------------------------------------------------------------
 *  TRAIN: Ação ao clicar no botão 'Train'
 *-----------------------------------------------------------------------------*/
void MainWindow::trainButtonClick()
{
    trainHopfield();
    isTrained = true;
    if(isTrained && isLoaded)
        testButton->setEnabled(true);
}

/*-----------------------------------------------------------------------------
 *  TEST: Ação ao clicar no botão 'Test'
 *-----------------------------------------------------------------------------*/
void MainWindow::testButtonClick()
{
    testHopfield();
}

/*-----------------------------------------------------------------------------
 *  TRAIN: Ação quando muda a seleção da lista de padrões
 *-----------------------------------------------------------------------------*/
void MainWindow::trainPatternItemChange()
{
    drawPattern(trainMemory, trainPatternList, trainPatternView);
}

/*-----------------------------------------------------------------------------
 *  TEST: Ação quando muda a seleção da lista de padrões
 *-----------------------------------------------------------------------------*/
void MainWindow::testPatternItemChange()
{
    drawPattern(testMemory, testPatternList, testInPatternView);
    iterationsLabel->clear();
    testOutPatternView->clear();
}

/*-----------------------------------------------------------------------------
 *  TRAIN: Ação ao selecionar um arquivo
 *-----------------------------------------------------------------------------*/
void MainWindow::trainFileSelected(const QString& file)
{
    if(loadMemory(file, trainMemory, trainPatternList))
    {
        trainButton->setEnabled(true);
        trainClearButton->setEnabled(true);
        trainPrevButton->setEnabled(true);
        trainNextButton->setEnabled(true);
    }
}

/*-----------------------------------------------------------------------------
 *  TEST: Ação ao selecionar um arquivo
 *-----------------------------------------------------------------------------*/
void MainWindow::testFileSelected(const QString& file)
{
    if(loadMemory(file, testMemory, testPatternList))
    {
        isLoaded = true;
        if(isTrained && isLoaded)
            testButton->setEnabled(true);
        testClearButton->setEnabled(true);
        testPrevButton->setEnabled(true);
        testNextButton->setEnabled(true);
    }
}

